package hackathon1212;

import java.security.Principal;

import javax.persistence.EntityManager;
import javax.persistence.Query;

public class UserDAO {

	/*
	 * Follow the singleton pattern
	 */
	private static UserDAO singleton;
	public synchronized static UserDAO get(){
		if(singleton == null){
			singleton = new UserDAO();
		}
		return singleton;
	}
	private UserDAO(){

	}
	
	public boolean allowed(final Principal p){
		if(p== null){
			return false;
		}		
		final EntityManager em = EMF.get().createEntityManager();
		try{
			final Query q = em.createQuery("select user from User user where " +
											"user.name = :NAME ");
			q.setParameter("NAME", p.getName());
			return q.getResultList().size() > 0;
		} finally {
			em.close();
		}
	}
	
}
